package com.xike.microemailserver.util;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.util.HashMap;

/**
 * Auther: wuson  <br/>
 * Date: 2021/5/2:17:33  <br/>
 * Description:
 */
public class MailUtil {
    @Resource
    JavaMailSenderImpl mailSender;


    public void sendEmail(HashMap map) throws  Exception{
        //创建一个复杂的消息邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        helper.setSubject((String) map.get("subject"));//邮件主题
        helper.setText((String) map.get("text"),true);//使用html，邮件内容
        helper.setTo((String) map.get("to"));//收件地址
        helper.setFrom((String) map.get("from"));//发件人
        String filePath = (String) map.get("file");//附件路径
        if(!StringUtils.isEmpty(filePath)){
            FileSystemResource fileSystemResource = new FileSystemResource(filePath);
            helper.addAttachment((String) map.get("newName"), fileSystemResource);
        }
        mailSender.send(mimeMessage);

    }

}
